package com.huifaapp.mall.view;

import android.content.Context;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.huifaapp.mall.R;
import com.huifaapp.mall.activity.JDcomHotlistBean;
import com.huifaapp.mall.adapter.Tmall_homepageAdapter;
import com.huifaapp.mall.bean.JDcomjPlistBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 1.类的用途
 * 2.@author zf
 * 3.@date 2017/12/6 14:16
 */

public class CustomViewPager extends RelativeLayout implements View.OnClickListener{


    private List<JDcomHotlistBean.DataBean.ListBean> imgList;
    private ViewPager vp;
    private LinearLayout ll_out;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

        }
    };
    private MyViewPagerAdapter adapter;
    private TextView title1;
    private TextView jian1;
    private TextView price1;
    private TextView go_shop1;
    private TextView title2;
    private TextView jian2;
    private TextView price2;
    private TextView go_shop2;
    private TextView title3;
    private TextView jian3;
    private TextView price3;
    private TextView go_shop3;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomViewPager(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //初始化控件
    private void init() {
        View view = inflate(getContext(), R.layout.custom_viewpager, null);
        //添加布局
        addView(view);
        vp = (ViewPager) view.findViewById(R.id.vp);
        ll_out = (LinearLayout) view.findViewById(R.id.ll_out);

    }

    //初始化小圆点
    private void initPoint() {
        for (int i = 0; i < imgList.size(); i++) {
            View view = new View(getContext());
            view.setBackgroundResource(R.drawable.point_bg);
            //动态生成小圆点
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15, 15);
            params.rightMargin = 20;
            ll_out.addView(view, params);
        }
        //给第一个选中
        View view = ll_out.getChildAt(0);
       view.setEnabled(true);
    }

    //获取viewpager数据源
    private void initVp() {
        adapter = new MyViewPagerAdapter();
        vp.setAdapter(adapter);

        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                int count = ll_out.getChildCount();
                Log.e("come",position+"");
                for (int i = 0; i < count; i++) {
                    View view = ll_out.getChildAt(i);
                    //设置小圆点属性
                    view.setEnabled(i == (position %= imgList.size()) ? false : true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public void setData(List<JDcomHotlistBean.DataBean.ListBean> imgList) {
        this.imgList = imgList;
        init();
        initVp();
        initPoint();
    }
   //自动轮播
    public void startSwith(){
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                int currentItem = vp.getCurrentItem();
                currentItem++;
                vp.setCurrentItem(currentItem);
                handler.postDelayed(this,5000);
            }
        },5000);
    }
    public void stopSwith(){
        handler.removeCallbacksAndMessages(null);
    }

    @Override
    public void onClick(View v) {
        if (mMyItemclickListener != null) {
            mMyItemclickListener.itemcCustom(v, (Integer) v.getTag());
        }
    }
    public interface MyItemCustomListener {
        void itemcCustom(View view, int position);
    }
    public MyItemCustomListener mMyItemclickListener;

    public void setmMyItemCustomListener(MyItemCustomListener mMyItemCustomListener) {
        this.mMyItemclickListener = mMyItemCustomListener;
    }
    private class MyViewPagerAdapter extends PagerAdapter {

        private ImageView image1;
        private ImageView image2;
        private ImageView image3;
        private View show_view;
        private RelativeLayout show_relativeLayout;

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            int i = position % imgList.size();
            show_view = View.inflate(getContext(), R.layout.show_data,null);
            show_relativeLayout = (RelativeLayout) show_view.findViewById(R.id.show_RelativeLayout);
            deleteStyle();
            image1 = (ImageView) show_view.findViewById(R.id.show_image1);
            image2 = (ImageView) show_view.findViewById(R.id.show_image2);
            image3 = (ImageView) show_view.findViewById(R.id.show_image3);
                image1.setScaleType(ImageView.ScaleType.FIT_XY);
                image2.setScaleType(ImageView.ScaleType.FIT_XY);
                image3.setScaleType(ImageView.ScaleType.FIT_XY);
            title1 = (TextView)show_view.findViewById(R.id.show_image1_title);
            jian1 = (TextView)show_view.findViewById(R.id.jian);
            price1 = (TextView)show_view.findViewById(R.id.price);
            go_shop1 = (TextView)show_view.findViewById(R.id.go_shop);

            title2 = (TextView) show_view.findViewById(R.id.show_image2_title);
            jian2 = (TextView) show_view.findViewById(R.id.jian2);
            price2 = (TextView) show_view.findViewById(R.id.price2);
            go_shop2 = (TextView) show_view.findViewById(R.id.go_shop2);

            title3 = (TextView) show_view.findViewById(R.id.show_image3_title);
            jian3 = (TextView) show_view.findViewById(R.id.jian3);
            price3 = (TextView) show_view.findViewById(R.id.price3);
            go_shop3 = (TextView) show_view.findViewById(R.id.go_shop3);
            switch (i){
                case 0:
                    Picasso.with(getContext()).load(imgList.get(i).getMainImageUrl()).into(image1);
                        title1.setText(imgList.get(i).getGoodsName());
                        price1.setText("¥ "+imgList.get(i).getPrice());
                        go_shop1.setText("已抢"+imgList.get(i).getTotalSales()+"件");
                    if (imgList.get(i).getCouponMoney().equals(null)||imgList.get(i).getCouponMoney().equals("")){
                        jian1.setText("¥"+0);
                    }else {
                        jian1.setText("¥"+imgList.get(i).getCouponMoney());
                    }


                    Picasso.with(getContext()).load(imgList.get(i+1).getMainImageUrl()).into(image2);
                    title2.setText(imgList.get(i+1).getGoodsName());
                    price2.setText("¥ "+imgList.get(i+1).getPrice());
                    if (imgList.get(i+1).getCouponMoney().equals(null)||imgList.get(i+1).getCouponMoney().equals("")){
                        jian2.setText("¥"+0);
                    }else {
                        jian2.setText("¥"+imgList.get(i+1).getCouponMoney());
                    }
                    go_shop2.setText("已抢"+imgList.get(i+1).getTotalSales()+"件");


                    Picasso.with(getContext()).load(imgList.get(i+2).getMainImageUrl()).into(image3);
                    title3.setText(imgList.get(i+2).getGoodsName());
                    price3.setText("¥ "+imgList.get(i+1).getPrice());
                    if (imgList.get(i).getCouponMoney().equals(null)||imgList.get(i).getCouponMoney().equals("")){
                        jian3.setText("¥"+0);
                    }else {
                        jian3.setText("¥"+imgList.get(i+2).getCouponMoney());
                    }
                    go_shop3.setText("已抢"+imgList.get(i+2).getTotalSales()+"件");

                    break;
                case 1:
                    Picasso.with(getContext()).load(imgList.get(i+3).getMainImageUrl()).into(image1);
                    title1.setText(imgList.get(i+3).getGoodsName());
                    price1.setText("¥ "+imgList.get(i+3).getPrice());
                    go_shop1.setText("已抢"+imgList.get(i+3).getTotalSales()+"件");
                    if (imgList.get(i+3).getCouponMoney().equals(null)||imgList.get(i+3).getCouponMoney().equals("")){
                        jian1.setText("¥"+0);
                    }else {
                        jian1.setText("¥"+imgList.get(i+3).getCouponMoney());

                    }

//                    Picasso.with(getContext()).load(imgList.get(i+4).getMainImageUrl()).into(image2);
//                    title2.setText(imgList.get(i+4).getGoodsName());
//                    price2.setText("¥ "+imgList.get(i+4).getPrice());
//                    go_shop2.setText("已抢"+imgList.get(i+4).getTotalSales()+"件");
//                    if (imgList.get(i+4).getCouponMoney().equals(null)||imgList.get(i+4).getCouponMoney().equals("")){
//                        jian2.setText("¥"+0);
//                    }else {
//                        jian2.setText("¥"+imgList.get(i+4).getCouponMoney());
//
//                    }
//
//                    Picasso.with(getContext()).load(imgList.get(i+5).getMainImageUrl()).into(image3);
//                    title3.setText(imgList.get(i+5).getGoodsName());
//                    price3.setText("¥ "+imgList.get(i+5).getPrice());
//                    go_shop3.setText("已抢"+imgList.get(i+5).getTotalSales()+"件");
//                    if (imgList.get(i+5).getCouponMoney().equals(null)||imgList.get(i+5).getCouponMoney().equals("")){
//                        jian3.setText("¥"+0);
//                    }else {
//                        jian3.setText("¥"+imgList.get(i+5).getCouponMoney());
//
//                    }
//                          break;
//                case 2:
//                    Picasso.with(getContext()).load(imgList.get(i+6).getMainImageUrl()).into(image1);
//                    title1.setText(imgList.get(i+6).getGoodsName());
//                    price1.setText("¥ "+imgList.get(i).getPrice());
//                    go_shop1.setText("已抢"+imgList.get(i).getTotalSales()+"件");
//                    if (imgList.get(i+6).getCouponMoney().equals(null)||imgList.get(i+6).getCouponMoney().equals("")){
//                        jian1.setText("¥"+0);
//                    }else {
//                        jian1.setText("¥"+imgList.get(i+6).getCouponMoney());
//                    }
//
//
//                    Picasso.with(getContext()).load(imgList.get(i+7).getMainImageUrl()).into(image2);
//                    title1.setText(imgList.get(i+7).getGoodsName());
//                    price1.setText("¥ "+imgList.get(i+7).getPrice());
//                    go_shop1.setText("已抢"+imgList.get(i).getTotalSales()+"件");
//                    if (imgList.get(i+7).getCouponMoney().equals(null)||imgList.get(i).getCouponMoney().equals("")){
//                        jian1.setText("¥"+0);
//                    }else {
//                        jian1.setText("¥"+imgList.get(i+7).getCouponMoney());
//                    }

//                    Picasso.with(getContext()).load(imgList.get(i+8).getMainImageUrl()).into(image3);
//                    title1.setText(imgList.get(i+8).getGoodsName());
//                    price1.setText("¥ "+imgList.get(i+8).getPrice());
//                    go_shop1.setText("已抢"+imgList.get(i).getTotalSales()+"件");
//                    if (imgList.get(i+8).getCouponMoney().equals(null)||imgList.get(i+8).getCouponMoney().equals("")){
//                        jian1.setText("¥"+0);
//                    }else {
//                        jian1.setText("¥"+imgList.get(i+8).getCouponMoney());
//                    }
                    break;

            }

            container.addView(show_view);
            return show_view;
        }
       //删除样式
        public void deleteStyle(){
            TextView jian=(TextView)show_view.findViewById(R.id.jian);
            TextView jian2=(TextView)show_view.findViewById(R.id.jian2);
            TextView jian3=(TextView)show_view.findViewById(R.id.jian3);
            jian.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG |Paint.ANTI_ALIAS_FLAG);
            jian2.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG |Paint.ANTI_ALIAS_FLAG);
            jian3.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG |Paint.ANTI_ALIAS_FLAG);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
//              super.destroyItem(container, position, object);
                container.removeView((View) object);
        }
    }

}
